Ext.namespace("org.microsite.host");

org.microsite.host.HostEditor = Ext.extend(Ext.Window, {
    title : 'Edit/create host',
    closable : true,
    modal : true,
    width : 500,
    height : 400,
    layout : 'fit',
    iconCls : 'hosticon',
    
    data : null,
    parentObject : null,
    success : false,

    txtName : null,
    txtDescription : null,
    txtIP : null,
    txtURL : null,
    txtOS : null,
    txtOSVersion : null,
    txtJVM : null,
    txtJVMPath : null,
    txtDomain : null,
    cbAdmin : null,
    
    frm : null,

    initComponent : function() {
	    this.txtName = new Ext.form.TextField({
            name : 'name',
            fieldLabel : 'Host name',
            anchor : '100%',
            allowBlank : false,
            value : this.data ? this.data.name : '',
            emptyText : 'This field is required'
        });
	    this.txtDescription = new Ext.form.TextArea({
            name : 'description',
            fieldLabel : 'Description',
            value : this.data ? this.data.description : '',
            anchor : '100%'
        })
	    this.txtIP = new Ext.form.TextField({
            name : 'ip',
            fieldLabel : 'IP address',
            value : this.data ? this.data.ip : '',
            anchor : '100%',
            allowBlank : false,
            emptyText : 'This field is required'
        });
	    this.txtURL = new Ext.form.TextField({
            name : 'url',
            fieldLabel : 'External URL',
            value : this.data ? this.data.url : '',
            anchor : '100%'
        });
	    this.txtOS = new org.microsite.util.AutoField({
            service : HostRemote,
            columnName : 'os',
            
            name : 'os',
            fieldLabel : 'OS',
            value : this.data ? this.data.os : '',
            anchor : '100%',
            allowBlank : false,
            emptyText : 'This field is required'
        });
	    this.txtOSVersion = new org.microsite.util.AutoField({
            service : HostRemote,
            columnName : 'osVersion',
            
            name : 'osVersion',
            fieldLabel : 'OS version',
            value : this.data ? this.data.osVersion : '',
            anchor : '100%',
            allowBlank : false,
            emptyText : 'This field is required'
        });
	    this.txtJVM = new org.microsite.util.AutoField({
		    service : HostRemote,
		    columnName : 'jvm',
            
            name : 'jvm',
            fieldLabel : 'JVM',
            value : this.data ? this.data.jvm : '',
            anchor : '100%',
            allowBlank : false,
            emptyText : 'This field is required'
        });
	    this.txtJVMPath = new org.microsite.util.AutoField({
            service : HostRemote,
            columnName : 'jvmPath',
            
            name : 'jvmPath',
            fieldLabel : 'JVM path',
            value : this.data ? this.data.jvmPath : '',
            anchor : '100%',
            allowBlank : false,
            emptyText : 'This field is required'
        });
	    this.txtDomain = new org.microsite.util.AutoField({
            service : HostRemote,
            columnName : 'domain',
            
            name : 'domain',
            fieldLabel : 'Domain',
            value : this.data ? this.data.domain : '',
            anchor : '100%',
            allowBlank : false,
            emptyText : 'This field is required'
        });
        this.cbAdmin = new org.microsite.util.ObjectSelector({
            type : User,
            selectedValue : this.data ? this.data.admin : null,

            hiddenName : 'admin',
            fieldLabel : 'Host admin',
            anchor : '100%'
        });
        this.frm = new Ext.FormPanel({
            bodyStyle : 'padding: 5px',
            items : [this.txtName, this.txtDescription, this.txtIP, this.txtURL, this.txtOS,
                this.txtOSVersion, this.txtJVM, this.txtJVMPath, this.txtDomain, this.cbAdmin]
        });

        this.btnOK = new Ext.Button({
            text : 'OK',
            handler : this.onOKClicked,
            scope : this
        });
        this.btnCancel = new Ext.Button({
            text : 'Cancel',
            handler : this.onCancelClicked,
            scope : this
        });
        

        Ext.apply(this, {
            items : [this.frm],
            buttons : [this.btnOK, this.btnCancel]
        });

        org.microsite.host.HostEditor.superclass.initComponent.apply(this);

        this.addEvents({
            'change' : true
        });
    },
    
    onOKClicked : function(btn, e) {
        var host = Ext.apply(new Host(), this.frm.getForm().getValues(), this.data);
        if (this.parentObject)  host.group = this.parentObject.id;
        this.success = true;
        this.fireEvent('change', host);
        this.close();
    },
    
    onCancelClicked : function(btn, e) {
        this.close();
    }
});